<?php

namespace App\Admin\Controllers;

use App\Models\Order;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OrderController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('订单管理');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->name('工程名称');
            $grid->column('详情','详情')->display(function () {

                return <<<EOT
                    <a class="btn btn-xs btn-default grid-expand collapsed" data-inserted="0" data-key="{$this->id}" data-toggle="collapse" data-target="#grid-collapse-{$this->id}" aria-expanded="false">
                       <i class="fa fa-caret-right"></i> 详情
                    </a>
                    <template class="grid-expand-{$this->id}">
                    <div id="grid-collapse-{$this->id}" class="collapse">
                    <table class="table">
                    <thead><tr></tr></thead>
                    <tbody>
                        <tr>
                        <td>ID</td>
                        <td>{$this->id}</td>
                        </tr>
                    </tbody>
                    </table>
                    </div>
                    </template>      
EOT;

            });
            $grid->address('地址');
            $grid->budget('工程预算');
            $grid->contact_name('联系人');
            $grid->contact_phone('联系人电话');
            $grid->status('状态')->display(function ($value) {
                if ($value == 0) {
                    return "<span style='color:red'>未发布</span>";
                } else if ($value == 1) {
                    return "<span style='color:red'>待抢单</span>";
                } else if ($value == 2) {
                    return "<span style='color:red'>已接单</span>";;
                } else if ($value == 3) {
                    return "<span style='color:green'>已开始</span>";;
                } else if ($value == 4) {
                    return "<span style='color:grey'>已结束</span>";;
                } else if ($value == 5) {
                    return "<span style='color:grey'>已结算</span>";;
                }
            });

            //操作
            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });

            //关掉批量删除
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->created_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Order::class, function (Form $form) {
            $form->disableSubmit();
            $form->disableReset();

            $form->display('id', 'ID');
            $form->display('name', '工程名称');
            $form->map('latitude', 'longitude', '位置');

            $form->display('created_at', '创建时间');
//            $form->display('updated_at', 'Updated At');
        });
    }
}
